package fr.lip6.meta.ple.plextraction;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.gmt.modisco.java.Model;

import artefact.generic.Artefact;
import fr.lip6.meta.ple.plextraction.config.Configuration;
import fr.lip6.meta.ple.plextraction.config.ExtensionType;
import fr.lip6.meta.ple.plextraction.config.ExtractionParameters;
import fr.lip6.meta.tools.ModiscoJavaModelLoader;

public class MainTestBank4Xmi {
	/**
	 * @param args
	 */
	public static void checkArgs(String[] args) {
		ExtensionType extensionType = null;
		
		if (args.length == 1 || args.length == 2) {
			String extension = args[0];

			if (extension.equalsIgnoreCase("aspectj")) {
				extensionType = ExtensionType.AJ;
			} else if (extension.equalsIgnoreCase("featurehouse")) {
				extensionType = ExtensionType.FH;
			} else {
				extensionType = ExtensionType.FH;
			}
		} else {
			extensionType = ExtensionType.FH;
		}
		
		initConfig(extensionType);
	}
	
	public static void initConfig(ExtensionType e) {
		Configuration config = Configuration.getInstance();
		
		config.setExtensionType(e); // generation context created in same time
		config.setParameterValue(ExtractionParameters.XMI_DIR, "generated/bank4");
		config.setParameterValue(ExtractionParameters.CONFIG_DIR, "PLE-Config");
	}
	
	public static void setConfigModels(Map<String, Model> models) {
		Configuration config = Configuration.getInstance();
		
		config.setModels(models);
	}
	
	public static void generateConfigFile(String configFilename) {
		Configuration config = Configuration.getInstance();
		String configDir = config.getParameterValue(ExtractionParameters.CONFIG_DIR);
		
		System.out.println("generateConfigFile()");
		
		try {
			Configuration.sauverConfig(configDir+"/"+configFilename, config);
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
	
	public static void setConfig(String configPath) {
		try {
			System.out.println("setConfig()");
			Configuration.chargerConfig(configPath);
		} catch (IOException e) {
			System.err.println(e.getMessage());
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String xmiDir = "bank4";
		ArrayList<Artefact> artefacts = new ArrayList<Artefact>();
		Map<String, Model> models = new HashMap<String, Model>();
		String[] products = {"",
			"Product1Bank",
			"Product2Bank",
			"Product3Bank",
			"Product4Bank",
			"Product5Bank",
			"Product6Bank",
			"Product7Bank",
			"Product8Bank",
		};
		final int N = args.length > 1 ? Integer.parseInt(args[1]) : 4;
		
		checkArgs(args);
		setConfigModels(models);
		//generateConfigFile("PLEConfig1");
		//setConfig("PLE-Config/PLEConfig1");
		
		System.out.println("Test");
		
		for (int i=1; i<=N; i++) {
			ModiscoJavaModelLoader modiscoLoader = new ModiscoJavaModelLoader();
			String fileName = "generated/"+xmiDir+"/"+products[i]+"_java.xmi";
			Model model = ModiscoJavaModelLoader.loadModiscoJavaModel(fileName);
			Artefact artefact;
			
			modiscoLoader.createArtefact(model, fileName);

		 	artefact = modiscoLoader.getProduct();
			artefact.setId(i);
			artefacts.add(artefact);
			
			models.put(fileName, model);
		}
		
		Extract.extract(artefacts, xmiDir, products,"");
		
		System.out.println("Fin ("+Configuration.getInstance().getExtensionType()+")");
	}
}